//
//  EMJRefreshFooter.m
//  Common
//
//  Created by xiang ji on 2018/6/27.
//

#import "EMJRefreshFooter.h"

@interface EMJRefreshFooter ()

@property (strong, nonatomic) NSMutableArray *GIFImages;
@property (weak, nonatomic) UIImageView *loadingImageV;
@property (weak, nonatomic) UILabel *label;

@end

@implementation EMJRefreshFooter

- (void)prepare
{
    [super prepare];
    
    // 设置控件的高度
//    self.mj_h = 88;
    self.mj_h = 72.f;
    
//    UIImageView *logoImgv = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"gif_00000"]];
//    logoImgv.contentMode = UIViewContentModeScaleAspectFit;
//    [self addSubview:logoImgv];
//    self.loadingImageV = logoImgv;
//    self.loadingImageV.animationImages = self.GIFImages;
//    self.loadingImageV.animationDuration = 0.6f;
//    self.loadingImageV.animationRepeatCount = 0;
    
    UILabel *label = [[UILabel alloc] init];
    label.textColor = [UIColor colorWithRed:114/255.0 green:114/255.0 blue:114/255.0 alpha:1.0f];
    label.font = [UIFont boldSystemFontOfSize:14];
    label.textAlignment = NSTextAlignmentCenter;
    [self addSubview:label];
    self.label = label;
    
    UIImageView *logoImgv = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"loading_gif_plus"]];
    logoImgv.contentMode = UIViewContentModeScaleAspectFit;
    [self addSubview:logoImgv];
    self.loadingImageV = logoImgv;
}

/**
 *  摆放子控件
 */
- (void)placeSubviews
{
    [super placeSubviews];
    
    self.loadingImageV.center = CGPointMake(self.mj_w * 0.5 , self.mj_h  * 0.5 + 7);
    self.label.bounds = self.bounds;
    self.label.center = CGPointMake(self.mj_w * 0.5 , self.mj_h  * 0.5);
}

#pragma mark 监听scrollView的contentOffset改变
- (void)scrollViewContentOffsetDidChange:(NSDictionary *)change {
    [super scrollViewContentOffsetDidChange:change];
}

#pragma mark 监听scrollView的contentSize改变
- (void)scrollViewContentSizeDidChange:(NSDictionary *)change {
    [super scrollViewContentSizeDidChange:change];
}

#pragma mark 监听scrollView的拖拽状态改变
- (void)scrollViewPanStateDidChange:(NSDictionary *)change {
    [super scrollViewPanStateDidChange:change];
}

#pragma mark 监听拖拽比例（控件被拖出来的比例）
- (void)setPullingPercent:(CGFloat)pullingPercent {
    [super setPullingPercent:pullingPercent];
//    [self logoLoadingScaleRotatAnimation:pullingPercent];
}

#pragma mark 监听控件的刷新状态
- (void)setState:(MJRefreshState)state {
    MJRefreshCheckState;
    
    switch (state) {
        case MJRefreshStateIdle:
            [self stopAniamting];
            [self performSelector:@selector(hiddenResultLab) withObject:nil afterDelay:1.0];
            break;
        case MJRefreshStatePulling:
            [self stopAniamting];
            break;
        case MJRefreshStateRefreshing:
            [self startAnimating];
            break;
        default:
            break;
    }
}

- (void)hiddenResultLab
{
    self.label.hidden = YES;
}
//开始无穷旋转
- (void)startAnimating
{
    self.loadingImageV.hidden = NO;
    
//    [self.loadingImageV startAnimating];
//    [self.loadingImageV sizeToFit];
    
    self.label.hidden = YES;
    [self.loadingImageV setTransform:CGAffineTransformMakeScale(1.0f, 1.0f)];
    CABasicAnimation *rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
    rotationAnimation.toValue = @(M_PI * 2.0);
    rotationAnimation.duration = 0.6f;
    rotationAnimation.autoreverses = NO;
    rotationAnimation.repeatCount = HUGE_VALF;
    rotationAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
    [self.loadingImageV.layer addAnimation:rotationAnimation forKey:@"rotationAnimation"];
    [self.loadingImageV sizeToFit];
}

- (void)stopAniamting
{
    self.loadingImageV.hidden = YES;
    self.label.hidden = NO;
//    [self.loadingImageV stopAnimating];
    [self.loadingImageV.layer removeAnimationForKey:@"rotationAnimation"];
}

//旋转,放大动画
- (void)logoLoadingScaleRotatAnimation:(CGFloat)pullingPercent {
    //高度：
    float y = (1 - pullingPercent ) * self.mj_h / 2;
    //角度：
//    float rotationAngle = M_PI * 2 * pullingPercent; // 目前不需要角度
    
    self.loadingImageV.hidden = NO;   //有动画，开始显现
    
    CGAffineTransform t1 = CGAffineTransformMakeTranslation(0, MAX(y, 0));
    CGAffineTransform t2 = CGAffineTransformScale(t1, MIN(pullingPercent, 1.0f), MIN(pullingPercent, 1.0f));
    self.loadingImageV.transform = CGAffineTransformRotate(t2, 0.0f);
}

- (void)endWithResult:(RefreshResultType)result
{
    if (self.state != MJRefreshStateRefreshing) return;
    [self stopAniamting];
    switch (result) {
        case RefreshResultFailure:
            self.label.text = @"刷新失败";
            break;
        case RefreshResultSuccess:
            self.label.text = @"刷新成功";
            break;
        case RefreshResultFinal:
            self.label.text = @"已加载全部";
            break;
        case RefreshResultNoMore:
            self.label.text = @"我们是有底线的";
            break;
        default:
            break;
    }
    [self setState:MJRefreshStateIdle];
}

- (NSMutableArray *)GIFImages
{
    if (!_GIFImages) {
        NSArray *images = @[@"gif_00000", @"gif_00001", @"gif_00002", @"gif_00003", @"gif_00004", @"gif_00005", @"gif_00006", @"gif_00007", @"gif_00008", @"gif_00009", @"gif_00010", @"gif_00011", @"gif_00012", @"gif_00013", @"gif_00014"];
        NSMutableArray *arr = [NSMutableArray array];
        for (int i = 0; i < images.count; i++) {
            UIImage *img = [UIImage imageNamed:images[i]];
            [arr addObject:img];
        };
        _GIFImages = arr;
    }
    return _GIFImages;
}

@end
